package com.stu.stusystem.controller.system;

import com.github.pagehelper.PageInfo;
import com.stu.stusystem.common.ApiResult;
import com.stu.stusystem.service.LoginService;
import com.stu.stusystem.service.dto.RegisterDTO;
import com.stu.stusystem.service.system.UserManagerService;
import com.stu.stusystem.service.system.dto.UserManagerDTO;
import com.stu.stusystem.service.system.dto.UserManagerQuery;
import com.stu.stusystem.service.system.vo.StuUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author cxt
 * @date 2020/9/21
 * 用户管理：根据权限查询用户列表
 */
@Api(tags = "用户管理")
@RestController
@Slf4j
@RequestMapping("/manager/stu")
public class UserManagerController {

    private UserManagerService userManagerService;

    private LoginService loginService;

    @GetMapping("/get")
    @ApiOperation("获取分页用户列表")
    public PageInfo<StuUserVO> getStuMangerPage(UserManagerQuery query) {
        return this.userManagerService.getStuMangerPage(query);
    }

    @PutMapping("/update")
    @ApiOperation("更新用户信息")
    public ApiResult<String> updateStuManager(@RequestBody UserManagerDTO dto) {
        this.userManagerService.updateStuManager(dto);
        return ApiResult.success(ApiResult.UPDATE);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户")
    public ApiResult<String> removeUser(@RequestParam("id") String id) {
        this.userManagerService.deleteStu(id);
        return ApiResult.success(ApiResult.DELETE);
    }

    @PostMapping("/save")
    @ApiOperation("新增用户")
    public ApiResult<String> save(@RequestBody RegisterDTO dto) {
        this.loginService.register(dto);
        return ApiResult.success(ApiResult.SAVE);
    }

    @ApiOperation("重置密码")
    @ResponseBody
    @GetMapping("/resetPassword")
    public ApiResult<String> resetPassword(@RequestParam("id") String id) {
        String s = userManagerService.resetPwd(id);
        return ApiResult.success(s);
    }

    @ApiOperation("获取全部用户列表")
    @ResponseBody
    @GetMapping("/whole/user")
    public List<Map<String, String>> getWhole(){
        return this.userManagerService.getWhole();
    }

    @Autowired
    public void setUserManagerService(UserManagerService userManagerService) {
        this.userManagerService = userManagerService;
    }

    @Autowired
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }
}
